<template>
	<view class="container">
		<u-navbar title='选择成员' :title-bold='true' :border-bottom='false'></u-navbar>


		<view class="list ">
			<view class="item top">
				<span>已有成员</span>
				<view class="edit" @click="goTo()">
					<image src="../../../static/icon/edit.png" mode=""></image>
					<span>编辑</span>
				</view>
			</view>
			<view class="item" v-for="(item,index) in list" :key="index">
				<view class="card" @click="choose(item)">
					<view class="left">
						<!-- <image class="avatar" :src="item.avatar" ></image> -->
						<view class="avatar">
							{{item.username.substring(0, 1)}}
						</view>
						<view class="user">
							<view class="info">
								<span class="name">{{item.username}}</span>
								<span class="age">{{item.age}}</span>
								<span class="sex">{{item.gender_text}}</span>
							</view>
							<view class="info ">
								<span>联系电话</span>
								<span>{{item.mobile}}</span>
							</view>
						</view>
					</view>

					<view class="right" :class="item.bind?'choose':''"> </view>

				</view>
			</view>

		</view>
		<!--  -->
		<view class="click submit" @click="save">
			<!-- bind_num 已绑定数量 可绑定数量 people -->
			确认绑定（{{detail.bind_num||0}}/{{detail.people||0}}）
		</view>
		<!-- 提示 -->
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		bindMember,
		bindMemberDetail,
		memberList,
		protectDetail
	} from '@/api/index.js'
	export default {
		data() {
			return {
				list: [],
				detail: {},

			}
		},

		onLoad(e) {
			this.detail.id = e.id
		},
		onShow() {
			this.init()
		},
		methods: {
			init() {
				this.getBind()
			},
			goTo() {
				uni.navigateTo({
					url: '/sub/center/mine/member'
				})
			},
			choose(item) {
				// bind_num 已绑定数量 可绑定数量 people
				if (!item.binded) {
					item.bind = !item.bind
					if (item.bind) {
						if (this.detail.people > this.detail.bind_num) {
							this.detail.bind_num++
						} else {
							this.$refs.uToast.show({
								title: '只能绑定' + this.detail.people + '个！'
							})
							item.bind = !item.bind
						}
					} else {
						this.detail.bind_num--
					}
				}


			},
			save() {
				let that = this,
					ids = '';
				that.list.forEach(item => {
					if (item.bind) {
						ids = ids + item.id + ','
					}
				})
				ids = ids.substring(0, ids.length - 1)


				bindMember({
					'id': that.detail.id,
					'member_ids': ids
				}).then(res => {
					if (res.code == 1) {
						that.init()
						that.$refs.uToast.show({
							title: res.msg
						})
					} else {
						that.$refs.uToast.show({
							title: res.msg
						})
					}
				})
			},
			getBind() {
				let that = this
				let user = that.ws.getUserInfo()
				if (user.job == 2) {
					protectDetail({
						'id': that.detail.id
					}).then(res => {
						if (res && res.code == 1) {
							if (res.data.detail.people == res.data.detail.bind_num) {

								setTimeout(() => {
									uni.navigateBack({
										delta: 1
									})
								}, 1500)

							} else {
								that.detail = res.data.detail
								that.getMemberList()
							}

						} else {
							that.$refs.uToast.show({
								title: res.msg
							})
						}
					})
				} else {
					bindMemberDetail({
						'id': that.detail.id
					}).then(res => {
						if (res && res.code == 1) {
							if (res.data.detail.people == res.data.detail.bind_num) {
								that.$refs.uToast.show({
									title: '绑定成功！'
								})
								setTimeout(() => {
									uni.navigateBack({
										delta: 1
									})
								}, 1500)

							} else {
								that.detail = res.data.detail
								that.getMemberList()
							}

						} else {
							that.$refs.uToast.show({
								title: res.msg
							})
						}
					})
				}

			},

			getMemberList(id) {
				let that = this
				memberList().then(res => {
					if (res && res.code == 1) {
						that.list = res.data.list
						let binds = []
						binds = that.detail.member_info
						that.list.forEach(itm => {
							that.$set(itm, 'bind', false)
							if (binds.length > 0) {
								binds.forEach(item => {
									if (item.id == itm.id) {
										itm.binded = true
										that.$set(itm, 'bind', true)
									}
								})
							}
						})

						// console.log(that.detail);
						console.log(that.list);
					} else {
						that.$refs.uToast.show({
							title: res.msg
						})
					}
				})
			}
		}
	}
</script>

<style lang="less" scoped>
	.container {
		height: 100vh;
		background-color: #EEE;
		position: relative;

		.list {
			padding-bottom: 70px;
		}

		.submit {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100vw;
			height: 60px;
			background-color: #6EC9E1;
			color: #FFF;
			text-align: center;
			line-height: 60px;
		}

		.top {
			display: flex;
			justify-content: space-between;
			align-items: center;

			.edit {
				image {
					height: 14px;
					width: 14px;
					margin-right: 10rpx;
				}
			}
		}


		.item {
			margin-top: 20rpx;
			padding: 0 30rpx;

			.card {
				background-color: #FFF;
				border-radius: 10rpx;
				padding: 20rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;

				.left {
					display: flex;
					align-items: center;

					.avatar {
						margin-right: 16rpx;
						min-width: 84rpx;
						min-height: 84rpx;
						width: 84rpx;
						height: 84rpx;
						border-radius: 50%;
						color: #FFF;
						font-size: 18px;
						background-color: #6EC9E1;
						display: flex;
						justify-content: center;
						align-items: center;
					}

					.user {
						.info {
							padding: 10rpx 0;

							span {
								opacity: 0.8;
								font-size: 14px;
								margin: 0 10rpx;
							}

							.name {
								opacity: 1;
								font-size: 16px;
							}

							.age,
							.sex {
								color: #6EC9E1;
								padding: 4rpx 8rpx;
								border-radius: 8rpx;
								background-color: rgba(229, 229, 229, 0.5);
							}
						}
					}
				}

				.right {
					border: 1px #E7E7E7 solid;
					margin-right: 20rpx;
					border-radius: 50%;
					height: 18px;
					width: 18px;
					display: flex;
					justify-content: center;
					color: #EEE;
				}

				.choose {
					background: url('@/static/choose.png');
					background-size: cover;
					background-repeat: no-repeat;
				}
			}
		}


	}
</style>